﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.UI.Models;
using ARR.Entities;
using System.Collections.ObjectModel;
using ARR.Shared;
using System.Windows.Input;
using ARR.UI.Commands;
using ARR.UI.Helpers;

namespace ARR.UI.ViewModels
{
    public class CategoryMatchViewModel : ViewModelBase
    {
        IArticlesImporterModel model;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand closingCommand;
        private string[] originalCategories;

        public CategoryMatchViewModel(IArticlesImporterModel model, CategoryMatch match, string[] originalCategories)
        {
            this.model = model;
            this.DisplayName = Labels.EquivalenciaCategorias;
            this.originalCategories = originalCategories;

            Match = match;
            Match.Name = string.Join(",", originalCategories);
        }


        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(param => Save(), p => SelectedCategory != null);
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(param => Cancel());
                }
                return cancelCommand;
            }
        }

        public ICommand ClosingCommand
        {
            get
            {
                if (closingCommand == null)
                {
                    closingCommand = new RelayCommand(param => Match.CategoryId = 0);
                }
                return closingCommand;
            }
        }

        public ObservableCollection<Category> Categories
        {
            get
            {
                return new ObservableCollection<Category>(model.Categories.OrderBy(x => x.Name));
            }
        }

        public ObservableCollection<CategoryRole> CategoryRoles
        {
            get
            {
                return new ObservableCollection<CategoryRole>(model.CategoryRoles);
            }
        }

        public ObservableCollection<CategoryUniverse> CategoryUniverses
        {
            get
            {
                return new ObservableCollection<CategoryUniverse>(model.CategoryUniverses);
            }
        }

        public CategoryMatch Match { get; set; }

        public Category SelectedCategory { get; set; }



        private void Save()
        {
            List<CategoryMatch> matches = new List<CategoryMatch>();

            foreach (string originalName in originalCategories)
            {
                matches.Add(new CategoryMatch 
                {
                    Name = originalName,
                    CategoryId = Match.CategoryId,
                    CountryId = Match.CountryId
                });
            }
            
            model.UpdateTempArticlesCategoryMatch(Match.CategoryId, originalCategories);

            model.SaveCategoryMatches(matches);

            WindowManager.CloseView(this);
        }

        private void Cancel()
        {
            Match.CategoryId = 0;
            WindowManager.CloseView(this);
        }
    }
}
